import java.util.ArrayList;
import java.util.Iterator;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Manager 
{    
    PageList page_list = new PageList();
    RepositoryList repositorys = new RepositoryList();
    private String language;
    private String start_page;
       
    public Manager(String language)
    {
        this.language = language;
        this.start_page = getStartPage(language);
        initRepositorysUrlList();
    }   
    private void initRepositorysUrlList()
    {
        Iterator repositorys_iterator = repositorys.getRepositoryList().iterator();
        while(repositorys_iterator.hasNext())
        {
            IRepositoryAbstract repository = (IRepositoryAbstract)repositorys_iterator.next();
            repository.initUrlLinks(start_page);
        }
    }
    public String getStartPage(String language)
    {
        return page_list.getStartPage(language);
    }
    public void addFileByCount(ArrayList<String> type_list, int count, int size)
    {
        for(int i = 0; i < count; i++)
        {
            addFileBySelectedType(type_list, size);
        }
    }
    public void addFileBySelectedType(ArrayList<String> type_list, int size)
    {
        Iterator type_list_iterator = type_list.iterator();
        while(type_list_iterator.hasNext())
        {
            String type = (String)type_list_iterator.next();
            String content = getContentByType(type, size);
            Long file_size = new Long(content.length());
            System.out.print("\n Type: " + type);
            System.out.print("\n Size: " + content.length());
            System.out.print("\n Content: " + content);
            System.out.print("\n Language: " + language);
            addFile(content, type, language, file_size);
        }
    }
    public String getContentByType(String type, int size)
    {
        ArrayList<IRepositoryAbstract> repository_list = (repositorys).getRepositoryList();
        Iterator repository_iterator = repository_list.iterator();  
        String content = "";
        while(repository_iterator.hasNext())
        {
            IRepositoryAbstract repository = (IRepositoryAbstract)repository_iterator.next();
            if(type.equals(repository.getType()))
            {
                content += getContentBySize(repository, size);
            }
        }
        return content;
    }
    public String getContentBySize(IRepositoryAbstract repository, int size)
    {
        String text = "";
        while(text.length() < size)
        {       
            String next_page = repository.getUrlLinks().get(0);
            repository.getUrlLinks().remove(0);
            repository.addUrlLinks(start_page);
            text += repository.getContent(next_page);
        }
        String result_string = text.substring(0, size);
        return result_string;
    }
    public void addFile(String text, String file_type, String language, Long size)
    {
        Session session = HibernateUtil.currentSession();        
        Transaction tx = session.beginTransaction();
        TableFile file = new TableFile(text, file_type, language, size);     
        session.save(file);
        session.flush();
        tx.commit();
        HibernateUtil.closeSession();
    }
}
